﻿// HostPane scrolling functions for Posh4DNN Module

Type.registerNamespace("PoshLib");

// Create the Constructor
PoshLib.HostPane = function(scrollContainer)
{
    this.bottomThreshold = 50;
    this.scrollContainer = scrollContainer;
}

// HostPane class functions
PoshLib.HostPane.prototype.scrollToEnd = function() {
    var scrollDiv = this.scrollContainer;
    if (scrollDiv.scrollHeight > scrollDiv.offsetHeight) 
		scrollDiv.scrollTop = scrollDiv.scrollHeight - scrollDiv.offsetHeight;
    scrollDiv = null;
}
    
PoshLib.HostPane.prototype.isScrollable = function() {
    var scrollDiv = this.scrollContainer;
    var currentHeight = 0;
    
    if (scrollDiv.scrollHeight > 0)
        currentHeight = scrollDiv.scrollHeight;
    else 
        if (scrollDiv.offsetHeight > 0)
            currentHeight = scrollDiv.offsetHeight;

    var Result = (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold);
    scrollDiv  = null;
    
    return Result;
}
    

PoshLib.HostPane.registerClass("PoshLib.HostPane", null);

var poshPane;

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);

function beginRequest(sender, args) {
    if (poshPane != null) 
        var scrolldiv = poshPane.scrollContainer;
}

function endRequest(sender, args) {
    if (poshPane == null) {
        init();
    }
    if (poshPane != null & poshPane.isScrollable() == true) {
            poshPane.scrollToEnd();            
    }
}

function init()
{
    var poshConsole = document.getElementById("Posh4DNNConsole");
    var poshTextArea = poshConsole.getElementsByTagName("TEXTAREA")[0];
    poshPane = new PoshLib.HostPane(poshTextArea);
}
    
if(typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

